package com.adminpanel.service;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import com.adminpanel.modelbean.UploadBean;
import com.adminpanel.modelbean.UserBean;
import com.adminpanel.modelbean.UserfileBean;

public class UploadImpl {

	//private UserBean userBean;
	UserBean userBean = new UserBean();
	
	
	//UploadBean uploadBean = new UploadBean();
	public boolean upload(UploadBean uploadBean,UserfileBean userfileBean,int user_id,String create_time) {
		FileOutputStream fos = null;
		try {

			//File f = new File("D:/upload_files/"+user_id+"/"+create_time);
			File f = new File(userfileBean.setSave_path("D:/upload_files/"+user_id+"/"+create_time));
			
			if (!f.exists()) {
				boolean result = f.mkdirs();

				if (!result) {
					System.out.println("Could not create dir");
				}

			}

			fos = new FileOutputStream("D:/upload_files/"+user_id+"/"+create_time+"/"+uploadBean.getFiledata().getOriginalFilename().toUpperCase());
			//fos = new FileOutputStream("D:/upload_files/"+user_id+"/"+uploadBean.getFiledata().getOriginalFilename().toUpperCase());
			
			fos.write(uploadBean.getFiledata().getBytes());
			fos.flush();
			return true;
		} catch (FileNotFoundException e) {

			e.printStackTrace();
			return false;
		} catch (IOException e) {

			e.printStackTrace();
			return false;
		} finally {
			try {
				if (fos != null) {
					fos.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

	}
}
